import axios from "axios";
import type { InternalAxiosRequestConfig } from 'axios'
import { showFailToast } from 'vant'

// 创建 axios 实例
const instance = axios.create({
    baseURL: 'http://121.89.205.189:3000/api'
})

const list: string[] = ['/cart/list', '/cart/add']

// 挂载拦截器
instance.interceptors.request.use((config: InternalAxiosRequestConfig<any>) => {
    if (list.indexOf(config.url || '') !== -1) {
        const token: string = window.localStorage.getItem('token') || ''
        config.headers.token = token
    }
    return config
})

instance.interceptors.response.use(res => {
    // 同意判断登录状态
    if (res.data.code === '10119') {
        // 没有登录
        showFailToast('您还没有登录')
    }
    return res.data
})

// 导出axios实例
export default instance